package com.aaa.sys.controller;


import com.aaa.vo.R;
import com.aaa.sys.entity.DictType;
import com.aaa.sys.service.DictTypeService;
import com.aaa.sys.vo.DictTypeVo;
import com.aaa.util.JwtUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author ykq
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/system/dictType")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    @PostMapping("findDictTypeAll/{page}/{limit}")
    public R findDictTypeAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody DictTypeVo dictTypeVo){
        Page<DictType> page1=this.dictTypeService.findDictTypeAll(page,limit,dictTypeVo);
        return R.ok().data("dictTypes",page1.getRecords()).data("total",page1.getTotal());
    }

    @PostMapping("saveDictType")
    public R saveDictType(@RequestBody DictType dictType, HttpServletRequest request){
        try {
            dictType.setSystemFlag("Y");
            dictType.setCreateTime(new Date());
            dictType.setCreateUser(JwtUtils.getMemberIdByJwtToken(request.getHeader("token")));
            boolean save = this.dictTypeService.save(dictType);
            return R.ok().message("添加数据字典类型成功");

        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("添加数据字典类型失败");
        }
    }


    @PostMapping("updateDictType")
    public R updateDictType(@RequestBody DictType dictType){
        try {
            boolean save = this.dictTypeService.updateById(dictType);
            return R.ok().message("修改数据字典类型成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("修改数据字典类型失败");
        }
    }

    @GetMapping("findByDictType/{dictTypeId}")
    public R findByDictType(@PathVariable Long dictTypeId){
        DictType dictType = this.dictTypeService.getById(dictTypeId);
        return R.ok().data("dictType",dictType);
    }


}

